from django.shortcuts import render
from django.contrib.auth import authenticate, login
from django.shortcuts import render, get_object_or_404
from django.shortcuts import get_list_or_404
from django.http import HttpResponse

from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.template.defaulttags import csrf_token

from django.template import RequestContext, loader
from django.views.decorators.csrf import csrf_exempt, csrf_protect
from django.contrib.auth.decorators import login_required
# Create your views here.

from tools import load_template
from tools import current_user
from tools import get_current_c_user
from tools import session_mixs

from user.forms import *
from isa.models import *

@csrf_exempt
@load_template("isa/home")
def home_view(request):
    # global template ,"there"
    user = current_user(request)
    cuser = None
    if user.is_active :
        cuser = get_current_c_user(request)

    

    if request.method == "GET":
        form = LoginForm()  
        context  = RequestContext(request,{
                'user':user,
                'form':form,
                'u':cuser,
            })
        return HttpResponse(template.render(context))
    if request.method == "POST":
        args = request.POST
        print(args)
        login_info = {
                'username':args['name'],
                'password':args['password'],
                }
        user = authenticate(**login_info)
        if user:
            if user.is_active:
                print("here ")
                login(request,user)
                name = user.username
                if not  name:
                    print("user is none")
                


                return HttpResponse(name)#,contenttypes="application/javascript")
            else:
                return HttpResponse("Anymouns")#,contenttypes="application/javascript")
        else:
            return HttpResponseRedirect(reverse("isa:home"))
@login_required
@load_template("isa/user")
def user_view(request,user_id):
    user = current_user(request)
    cuser = get_object_or_404(CUser,user=user)
    if request.method == "GET":
        questions = [get_object_or_404(Question,id=log.question_id)  for log in cuser.log_set.all()]       
        context  = RequestContext(request,{
                'u':cuser,
                'questions':questions,

            })
        return HttpResponse(template.render(context))

@login_required
@load_template("isa/questions")
def questions_view(request,type_id):
    user = current_user(request)
    cuser = get_current_c_user(request)

    if request.method == "GET":
        u = get_object_or_404(Question,id=type_id)
        all_question = session_mixs.get(request,"qc")
        print(all_question)
        if not all_question: 
            all_question = Question.objects.count()
            session_mixs.set(request,qc=all_question)

        title = u.title
        content = u.content 
        tags =[ tag.content for tag in  get_list_or_404(Tags,question=u)]
        file_path = u.files.url
        if_exist_next = False
        if_right = cuser.if_right_question(u.id)
        id  = u.id
        if_ahead = False
        if all_question > u.id:
            if_exist_next = u.id +1 
        
        if u.id != 1:
            if_ahead = u.id -1


        context  = RequestContext(request,{
                'title':title,
                'id':id,
                'content':content,
                'tags':tags,
                'file_path':file_path,
                'if_exist_next':if_exist_next,
                'if_ahead':if_ahead,
                'if_right':if_right,
                'user_name':user.username,
                'link':u.link,
                'u':cuser,
            })
        return HttpResponse(template.render(context))
    elif request.method == "POST":
        u = get_current_c_user(request)
        args = request.POST
        ques_id = args['question_id']
        print (ques_id)
        answer = args['answer']
        print (answer)
        such_quest = get_object_or_404(Question,id=ques_id)
        
        res = such_quest.if_right(answer)
        if res:
            if Log.if_exist(cuser,ques_id):
                log = Log(question_id=ques_id)

                u.log_set.add(log)
                u.save()
            return HttpResponseRedirect('/question/{}'.format(int(ques_id)+1))
        return HttpResponseRedirect('/question/{}'.format(int(ques_id)))


@login_required
@load_template('isa/question_panel')
def question_panel_view(request,main_tag):
    user = current_user(request)
    cuser = get_current_c_user(request)
    if request.method == "GET":
        questions = Question.get_by_tag(main_tag)
        questions_set = [ question for question in questions]
        for q in questions_set:
            q.tags = [tag.content for tag in   q.get_tags()]
        tags = Tags.get_all_tags()



        context = RequestContext(request,{
            'questions':questions_set,
            'tags':tags,
            'u':cuser,
        })

        return HttpResponse(template.render(context))




